package com.alen.sun.controller;

import com.alen.sun.bean.*;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@CrossOrigin
@RequestMapping("/messages")
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 生成消息
     * @param request
     * @param content
     * @return
     */
    @PostMapping("/add")
    @CrossOrigin
    public Result add(HttpServletRequest request ,@RequestBody Content content){
        log.info("1234567890messageAdd");
        Elderly elderly=(Elderly) request.getAttribute("elderly");
        System.out.println(elderly);
        System.out.println(content);
        messageService.add(elderly,content);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result list(@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        PageBean pageBean=messageService.list(pageNum,pageSize);
//        List<Message> messages=messageService.list();
        return Result.success(pageBean);
    }


    /**
     * 获取所有
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result listForEmployee(HttpServletRequest request){
        Employee employee=(Employee)request.getAttribute("employee");
        log.info("{}",employee);
        List<Message> messages=messageService.getMessagesForCaregiver(employee.getEmployeeId());
        if (messages!=null){
            return Result.success(messages);
        }else {
            return Result.error("暂无消息噢");
        }
    }

    /**
     * 修改消息状态
     * @param messageId
     * @return
     */
    @PutMapping("/{messageId}")
    public Result updateStatus(@PathVariable String messageId) {
        Boolean flag=messageService.updateStatus(messageId);
        if(flag){
            return Result.success();
        }else {
            return Result.error("接收错误，请稍后噢");
        }
    }

    /**
     * 消息查询
     * @param word
     * @return
     */
    @GetMapping("/search")
    public Result word(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestParam String word){
        PageBean pageBean = messageService.page(page,pageSize,word);
        log.info("search被使用");
        return Result.success(pageBean);
    }


    /**
     * 根据ID删除消息
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam String id){
        Boolean flag=messageService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("该信息还未被护工接收");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = messageService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }

    }
}
